﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Workflows.Manipulation
{
    public class StepContext
    {
        public StepContext(WFStep step, Workflow workflow)
        {
            if (workflow == null)
                throw new ArgumentNullException("workflow");

            _workflow = workflow;

            SetComponent(step);

        }

        private Workflow _workflow;

        public Workflow Workflow
        {
            get { return _workflow; }
        }

        private Stage _stage;

        public virtual Stage Stage
        {
            get { return _stage; }
        }

        private WFStep _step;

        public WFStep Step
        {
            get { return _step; }
        }

        protected void SetComponent(IWorkflowComponent value)
        {
            _step = null;
            _thread = null;
            _process = null;
            _stage = null;

            if (value != null)
            {
                _step = value as WFStep;
                _thread = _step == null ? value as WFThread : _step.Thread;
                _process = _thread == null ? value as WFProcess : _thread.Process;
                if (_step != null)
                    _stage = _workflow.Stages[_step];
            }
        }

        private WFThread _thread;

        public WFThread Thread
        {
            get { return _thread; }
        }

        private WFProcess _process;

        public WFProcess Process
        {
            get { return _process; }
        }

        internal StageOperationContract CreateContract()
        {
            return _stage.GetContract(_step);
        }
    }
}
